package operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 洪汤泽大帅哥
 * Date: 2024-11-02
 * Time: 10:56
 */
public class DelBook implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入您想要删除的书籍");
        String name = sc.nextLine();

        int index = -1;
        int i = 0;
        int currSize = bookList.getUsedSize();
        for (i = 0; i < currSize; i++) {
            Book book = bookList.getBooks(i);
            if (book.getName().equals(name)){
                index = i;
                break;
            }
        }
        if (i>currSize){
            System.out.println("没有你想要删除的书");
            return;
        }

        for (int j = index; j < currSize-1; j++) {
            Book temp = bookList.getBooks(j+1);
            bookList.setBooks(j,temp);
        }

        bookList.setUsedSize(currSize-1);

        bookList.setBooks(currSize-1,null);

        System.out.println("删除图书成功了！！");
    }
}
